import { login, gerUserList } from '@/api/user.js'
import { setToken, getToken, removeToken, setTokenTime } from '@/utils/auth.js'
import { authority } from '@/api/authority.js'
const state = {
  token: getToken(),
  // 左侧列表
  menu: [],
  // 用户数据
  userInfo: {}
}
const mutations = {

  // 设置token
  setToken(state, payload) {
    state.token = payload
    setToken(payload)
  },

  // 退出删除token
  // 退出登录
  logout(state) {
    state.token = null
    removeToken()
  },

  // 获取menu
  menuInfo(state, payload) {
    state.menu = payload
  },

  // 用户数据
  getUserManinfo(state, payload) {
    state.userInfo = payload
  }
}
const actions = {
  // 设置token
  async login(context, payload) {
    const res = await login(payload)
    setTokenTime()
    context.commit('setToken', res.token)
  },

  // 左侧数据
  async getMenuList(context) {
    const res = await authority()
    context.commit('menuInfo', res)
  },

  // 用户数据
  async getUserMan(context, payload) {
    const res = await gerUserList(payload)
    context.commit('getUserManinfo', res)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

